#!/usr/bin/python3
import os
from pathlib import Path

def create_optima_file():
    """
    创建汇总的最优解文件
    从 data/low-dimensional-optimum/ 和 data/large_scale-optimum/ 
    生成 data_converted/optima/optima.txt
    """
    # 输入目录
    optimum_dirs = [
        Path("data/low-dimensional-optimum"),
        Path("data/large_scale-optimum")
    ]
    
    # 输出目录和文件
    output_dir = Path("data_converted/optima")
    output_file = output_dir / "optima.txt"
    
    # 确保输出目录存在
    output_dir.mkdir(parents=True, exist_ok=True)
    
    # 收集所有最优解
    optima_data = {}
    
    for opt_dir in optimum_dirs:
        if not opt_dir.exists():
            print(f"目录不存在: {opt_dir}")
            continue
        
        print(f"处理目录: {opt_dir}")
        
        for opt_file in opt_dir.iterdir():
            if opt_file.is_file():
                try:
                    with open(opt_file, 'r') as f:
                        value = int(float(f.read().strip()))
                        optima_data[opt_file.name] = str(value)
                        print(f"  {opt_file.name}: {value}")
                except (ValueError, IOError) as e:
                    print(f"  错误处理文件 {opt_file.name}: {e}")
                    continue
    
    # 写入汇总文件
    with open(output_file, 'w', encoding='utf-8') as f:
        for filename, value in sorted(optima_data.items()):
            f.write(f"{filename},{value}\n")
    
    print(f"\n汇总文件已创建: {output_file}")
    print(f"总共 {len(optima_data)} 个最优解")

if __name__ == "__main__":
    create_optima_file() 